package com.ruoyi.accounts.service.impl;

import com.ruoyi.common.core.domain.store.EmpCustomDetail;
import com.ruoyi.accounts.domain.EmpProperty;
import com.ruoyi.accounts.mapper.AEmpCustomDetailMapper;
import com.ruoyi.accounts.mapper.EmpPropertyMapper;
import com.ruoyi.accounts.service.ITicketService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TicketServiceImpl implements ITicketService {
    
    private static final Logger log = LoggerFactory.getLogger(TicketServiceImpl.class);
    
    @Autowired
    private AEmpCustomDetailMapper aEmpCustomDetailMapper;
    @Autowired
    private EmpPropertyMapper empPropertyMapper;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTicket(String customID, int paramInt) {
        try {
            //查询emp_custom_detail表，获取customID对应的记录
            EmpCustomDetail empCustomDetail = aEmpCustomDetailMapper.selectByCustomID(customID);
            ///如果emp_custom_detail表中没有customID对应的记录，则返回false
            if (empCustomDetail == null) {
                log.error("找不到用户ID为" + customID + "的emp_custom_detail记录");
                return false;
            }
            //查询emp_property表，获取customID对应的记录
            EmpProperty empProperty = empPropertyMapper.selectByCustomID(customID);
            //如果emp_property表中没有customID对应的记录，则返回false
            if (empProperty == null) {
                log.error("找不到用户ID为" + customID + "的emp_property记录");
                return false;
            }

            //更新emp_custom_detail表的ticket字段
            empCustomDetail.setTicket(empCustomDetail.getTicket() + paramInt);
            int result1 = aEmpCustomDetailMapper.updateByCustomID(empCustomDetail);

            //更新emp_property表的ticket字段
            //转Long
            Long paramLong = Long.parseLong(String.valueOf(paramInt));
            empProperty.setTicket(empProperty.getTicket() + paramLong);
            int result2 = empPropertyMapper.updateEmpProperty(empProperty);

            //如果更新成功，则返回true
            return result1 > 0 && result2 > 0;
        } catch (Exception e) {
            log.error("更新票务失败", e);
            throw e; // 抛出异常以触发事务回滚
        }
    }
} 